package com.j.designpattern.strategy;

import lombok.Data;

public class StrategyPattern1 {

    public static void main(String[] args) {

        Context myContext = new Context();

        //使用A策略完成任务
        RealStrategyA realStrategyA = new RealStrategyA();
        myContext.setStrategy(realStrategyA);
        myContext.strategyMethod();
        //使用A策略完成任务
        RealStrategyB realStrategyB = new RealStrategyB();
        myContext.setStrategy(realStrategyB);
        myContext.strategyMethod();

    }
}

//抽象一个接口出来
interface Strategy {
    //策略方法
    public void strategyMethod();
}

//策略实现类A
class RealStrategyA implements Strategy {

    @Override
    public void strategyMethod() {
        System.out.println("i am strategy A");
    }
}
//策略实现类B
class RealStrategyB implements Strategy {

    @Override
    public void strategyMethod() {
        System.out.println("i am strategy B");
    }
}

//环境类
@Data
class Context {
    private Strategy strategy;

    public void strategyMethod() {
        strategy.strategyMethod();
    }
}
